Let's dive into the use cases
for Cloud Pub/Sub. Cloud Pub/Sub enables your application
to perform operations asynchronously. Traditionally, one service
or component of your application has to directly invoke another component
to perform an operation. The call is synchronous, so the caller has to wait
for the entire operation to finish. The application components
are tightly coupled because both must be running
at the same time. With Cloud Pub/Sub,
you can design your services to make asynchronous calls
and be loosely coupled. For example, in the diagram,
the order service acts as the publisher. It publishes messages
that contain order information to the orders topic
and returns immediately. The inventory service acts
as the subscriber. It might even be down when
the publisher sends messages. The subscriber can start
running a little later and process the messages
it is subscribed to. Cloud Pub/Sub
enables you to use a topic as a buffer to hold data
that is coming in rapidly. For example, you might have
multiple instances of a service generating
large volumes of data. A downstream service
that needs to process this data might become overwhelmed with the
high velocity and volume of data. To address this issue, instances
of your data-generating service can act as publishers and publish
the data to a Cloud Pub/Sub topic. Cloud Pub/Sub can reliably receive
and store large volumes of rapidly incoming data. The downstream service
can then act as a subscriber and consume the data
at a reasonable pace. Your application can even
automatically scale up the number of instances
of the subscriber to handle
increased volumes of data. The diagram shows many messages
with order information coming in
from various sources. The inventory service can subscribe
to these messages and process them at
a reasonable pace as appropriate. With Cloud Pub/Sub,
your application can fan out messages from one publisher
to multiple subscribers. Instead of being aware
of all services that might be interested
in a particular piece of data and making brittle point-to-point
connections with all of the services, the publisher can push messages
to a centralized Cloud Pub/Sub topic. Services that are interested
in the information can simply subscribe to the topic. For example, in the diagram,
information about orders is fanned out to the inventory
and payment services. Cloud Pub/Sub delivers
each message at least once. This means that a subscriber can
sometimes see duplicate messages. Implement your publisher
and subscriber in such a way that the application can performance
item-potent operations. The publisher can publish messages
with a unique ID. Your subscriber can use Cloud Datastore,
Cloud Firestore or Cloud Bigtable to store information about messages
that have already been processed. Use the unique identifier as a key.
Whenever a message is received, the subscriber can check
previously saved messages to see whether the incoming message
is new and needs to be processed. If the message
has already been processed, the subscriber can just discard
the duplicate message. Cloud Pub/Sub provides a highly
scalable messaging architecture. The scalability comes
with a trade-off. Message ordering
is not guaranteed. Where possible,
design your application to reduce or even eliminate dependencies
on message ordering. For example, message ordering
is not relevant in use cases where your application is collecting
statistics about events. You don't need to process the
statistics for related events in order. Similarly, if, say,
five of your contacts came online about
the same moment in time, you don't need to be notified
about their presence in the exact same order
that they came online. Then there are scenarios in which the
final order of messages is important, but the order in which the individual
messages are processed is not important. For example, log events
with time stamps may stream into the logging
service from various sources. The order of processing
each log event is not important. However, eventually your system
should enable you to view log events ordered by time stamp. Cloud Pub/Sub works well
naturally for use cases where order of message
processing is not important. Finally, there are scenarios
where messages must absolutely be processed in the
order in which they were published. For example, financial transactions
must be processed in order. Similarly, in a multiplayer online game,
if a player jumps over a wall, lands on a monster
and recovers the lost jewel, other players must see the events
in exactly the same order. You can implement ordered
processing of messages in one of the following ways.
In the first scenario, the subscriber knows the order
in which messages must be processed. The publisher can publish messages
with a unique ID. Your subscriber can use Cloud Datastore
to store information about messages that have been processed so far.
When a new message is received, the subscriber can check whether this
message should be processed immediately, or the unique ID indicates
that there are pending messages that should be processed first. For example, the inventory service
knows that orders should be processed in sequence
using the order ID as key. When messages with order IDs 103
and 104 are received, it checks the previously
processed orders and determines
that 102 is pending. It temporarily stores orders
103 and 104 and processes them all in order
when order 102 is received. In the second scenario, the subscriber checks
oldest unacknowledged message in Cloud
Monitoring metrics. The subscriber can store all messages
in a persistent data store. It can check indistinct subscription
oldest unacked message metric that the Cloud Pub/Sub
publishes to Stackdriver. The subscriber can compare
the time stamp of the oldest unacknowledged message
against the published time stamps of the messages
in Cloud Datastore. All messages published before
the oldest unacknowledged message are guaranteed
to have been received, so those messages can be removed
from the persistent data store and processed in order.